function check_name(name) {
    $("#stat").load(
        'index.php',
        {
            'name': name
        });
}

$(document).ready(function() {
    $("#check").click(function() {
        var name = $("[name='name']").val();
        check_name(name);
    });

    $("#grab-image").click(function() {
        var baseurl = $(this).parents('#grab-div').find('input[name="baseurl"]').val();
        var hint = $(this).parents('#grab-div').find('input[name="hint"]').val();
        var url = sprintf("index.php?url=%s&hint=%s",
                          encodeURIComponent(baseurl), hint);
        $("#grab-stat").attr('src', url);
    });

    $("#grab-text").click(function() {
        var baseurl = $(this).parents('#grab-div').find('input[name="baseurl-text"]').val();

        var url = sprintf("index.php?text-url=%s",
                          encodeURIComponent(baseurl));
        $("#grab-stat").attr('src', url);
    });

    $("#grab-stat").change(function(){
        alert('hi');
    });

    // Load proxy addresses in to dict
    proxy_dict = new Array();
    $("#exist-proxy-div span").each(function(i, val) {
        var addr = $(val).html();
        var octets = addr.split(".");
        proxy_dict[octets.slice(0, 3).join('.')] = addr;
    });

    // for (proxy in proxy_dict) {
    //     $("body").append(sprintf("%s->%s<br />", proxy, proxy_dict[proxy]));
    // }

    // hook addr-input text field
    $("#addr-input").keypress(function(event) {
        if (event.which == 46) {
            var value = $(event.target).val();
            if (value in proxy_dict) {
                $("#proxy-conflict").html("conflicting with " + proxy_dict[value]);
                $("#proxy-conflict").fadeIn();
            } else {
                $("#proxy-conflict").fadeOut('slow');
            }
        }
    });

    $("#addr-input").change(function(event) {
        var value = $(event.target).val();
        var octets = value.split(".");
        var value_key = octets.slice(0, 3).join('.');

        if (value_key in proxy_dict) {
            $("#proxy-conflict").html("conflicting with " + proxy_dict[value_key]);
            $("#proxy-conflict").fadeIn();
        } else {
            $("#proxy-conflict").fadeOut('slow');
        }
    });
});
